package com.chixing.service.impl;


import com.chixing.mapper.CarMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.mapper.StoreMapper;
import com.chixing.pojo.*;
import com.chixing.service.RentalCarService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author YHB
 * @date 2021/10/8  18:07
 */
@Service
public class RentalCarServiceImpl implements RentalCarService {

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private MyorderMapper orderMapper;


    //多条件查询
    //表：Car
    //商品列表（商品+品牌+价格+排挡+座位）
    @Override
    public ServerResponse getByConditions(String carBrandname, Double minCarCategoryStartprice, Double maxCarCategoryStartprice, String carMatic,
                                          Integer carSeatnum, Integer pageNum, Integer status) {


        CarExample carExample = new CarExample();
        CarExample.Criteria carcriteria = carExample.createCriteria();

        if (carBrandname.equals("-1"))
            carBrandname = null;
        if (minCarCategoryStartprice == -1)
            minCarCategoryStartprice = null;
        if (maxCarCategoryStartprice == -1)
            maxCarCategoryStartprice = null;
        if (carMatic.equals("-1"))
            carMatic = null;
        if (carSeatnum == -1)
            carSeatnum = null;
        if (status == 1)
            carcriteria.andStatusIsNull();

        if (carBrandname != null) {
            carcriteria.andCarBrandnameEqualTo(carBrandname);
        }
        if (minCarCategoryStartprice != null) {
            carcriteria.andCarCategoryStartpriceGreaterThanOrEqualTo(minCarCategoryStartprice);
        }
        if (maxCarCategoryStartprice != null) {
            carcriteria.andCarCategoryStartpriceLessThanOrEqualTo(maxCarCategoryStartprice);
        }
        if (carMatic != null) {
            carcriteria.andCarMaticEqualTo(carMatic);
        }
        if (carSeatnum != null) {
            carcriteria.andCarSeatnumEqualTo(carSeatnum);
        }


        PageHelper.startPage(pageNum, 5);
        List<Car> carList = carMapper.selectByExample(carExample);
        PageInfo<Car> pageInfo = new PageInfo<>(carList);

        System.out.println("carlist" + carList.size());
        if (carList != null && carList.size() > 0) {
            return ServerResponse.getSucccess(pageInfo);
        } else {
            return ServerResponse.getFail(null);
        }
    }


    //根据主键查门店
    @Override
    public ServerResponse getByStoreId(Integer storeId) {
        Store store = storeMapper.selectByPrimaryKey(storeId);
        if (store != null)
            return ServerResponse.getSucccess(store);
        else
            return ServerResponse.getFail(null);
    }


    //根据主键查车辆
    @Override
    public ServerResponse getByCarId(Integer carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        if (car != null)
            return ServerResponse.getSucccess(car);
        else

            return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCarDetail(Integer carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        if (car != null)
            return ServerResponse.getSucccess(car);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCarByStoreAndDate(Integer storeId, Date getCarTime, String carBrandname,Double minCarCategoryStartprice, Double maxCarCategoryStartprice,
                                               String carMatic, Integer carSeatnum, Integer pageNum, Integer status) {

        MyorderExample myorderExample;
        CarExample example = new CarExample();
        CarExample.Criteria carcriteria = example.createCriteria();
        example.createCriteria().andStoreIdEqualTo(storeId);
        if (carBrandname.equals("-1"))
            carBrandname = null;
        if (minCarCategoryStartprice == -1)
            minCarCategoryStartprice = null;
        if (maxCarCategoryStartprice == -1)
            maxCarCategoryStartprice = null;
        if (carMatic.equals("-1"))
            carMatic = null;
        if (carSeatnum == -1)
            carSeatnum = null;
        if (status == 1)
            carcriteria.andStatusIsNull();

        if (carBrandname != null) {
            carcriteria.andCarBrandnameEqualTo(carBrandname);
        }
        if (minCarCategoryStartprice != null) {
            carcriteria.andCarCategoryStartpriceGreaterThanOrEqualTo(minCarCategoryStartprice);
        }
        if (maxCarCategoryStartprice != null) {
            carcriteria.andCarCategoryStartpriceLessThanOrEqualTo(maxCarCategoryStartprice);
        }
        if (carMatic != null) {
            carcriteria.andCarMaticEqualTo(carMatic);
        }
        if (carSeatnum != null) {
            carcriteria.andCarSeatnumEqualTo(carSeatnum);
        }
        List<Car> cars = carMapper.selectByExample(example);
        List<Car> carList = new ArrayList<>();
        for (Car car : cars) {
            Integer carInventory = car.getCarInventory();
            if (carInventory > 0) {
                Car car1 = carMapper.selectByPrimaryKey(car.getCarId());
                carList.add(car1);
            } else {
                myorderExample = new MyorderExample();
                myorderExample.createCriteria().andCarIdEqualTo(car.getCarId());
                List<Myorder> myorders = orderMapper.selectByExample(myorderExample);
                myorderExample.clear();
                if (myorders.size() > 0) {
                    Myorder myorder = myorders.get(0);
                    if (getCarTime.compareTo(myorder.getOrderEndDate()) > 0) {
                        Car car2 = carMapper.selectByPrimaryKey(myorder.getCarId());
                        carList.add(car2);
                    }
                }
            }
        }
        if (carList.size() > 0)
            return ServerResponse.getSucccess(carList);
        return ServerResponse.getFail(null);
    }
}
